package demo.gl;

import java.nio.*;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


public class ball {
	private FloatBuffer fb;
	private ShortBuffer sb;
	private FloatBuffer cb;
	int numrow=10;
	int numcolumn=10;
	float rmax=0.1f;
	float rsmall=0.05f;
	int numradius=2*numrow*(numcolumn+1);
	float[] ball=new float[3*numradius];
	float[] color=new float[numradius*4];

	private int mauDo=1;
	private  int mauVang=2;
	private  int mauXanhLa=3;
	private  int mauXanhDT=4;
	private  int mauHong=5;
	private  int mauXanhLam=6;
	private int mau=mauHong;

	private FloatBuffer phanThan_Buffer;
	
	private FloatBuffer colorThan_Buffer;
	public ball(int x,int y,int color,int style)
	{
		
		
		init(style);
		float offsety=(float)(((x-6)*0.2)+0.2f);
		float offsetx=(float)(((y-6)*0.2));
		//System.out.println("x"+offsetx);
		//System.out.println("x"+offsety);
		for (int i=0;i<ball.length;i++)
		{
			if (i%3==0)
			{
				ball[i]+=offsetx;
			}
			if ((i-1)%3==0)
			{
				ball[i]+=-offsety;
				
			}
		}
		
		ByteBuffer bb=ByteBuffer.allocateDirect(ball.length*4);
		bb.order(ByteOrder.nativeOrder());
		fb=bb.asFloatBuffer();
		fb.put(ball);
		fb.position(0);
		drawColor(color);
	}
	public ball()
	{
		init(1);
	}
	public void init(int type)
	{
		float r=rmax;
		if(type==2)
		{
			r=rsmall;
		}
		
		float smallradian1=(float) (Math.PI/numrow);
		float smallradian2=(float) (2.0f*Math.PI/numcolumn);
		float radian1=0.0f;
		float radian2=0.0f;
		int z=0;
		for (int i=0;i<numrow;i++)
		{
			radian1=i*smallradian1;
			for (int j=0;j<=numcolumn;j++)
			{
				if (j==numcolumn)
					radian2=0.0f;
				else
					radian2=j*smallradian2;
				ball[z]=(float)(r*Math.cos(radian2)*Math.sin(radian1));
				z++;
				ball[z]=(float)(r*Math.sin(radian2)*Math.sin(radian1));
				z++;
				ball[z]=(float)(r*Math.cos(radian1));
				z++;
				ball[z]=(float)(r*Math.cos(radian2)*Math.sin(radian1+smallradian1));
				z++;
				ball[z]=(float)(r*Math.sin(radian2)*Math.sin(radian1+smallradian1));
				z++;
				ball[z]=(float)(r*Math.cos(radian1+smallradian1));
				z++;
			}
		}
		for (int i=0;i<ball.length;i++)
		{
			
			if ((i+1)%3==0)
			{
				ball[i]+=r+0.2f;
			}
			else
			{
				ball[i]+=0.1f;
			}
		}
		
		

		//drawColor(5);
	}
	public void drawColor(int idcolor)
	{
		float phamau=0.9f;
		float phatrang=1.0f;
		if (idcolor==1)
		{
		for(int k=0;k<numradius*4;k++){
			if(k%4==0)
				color[k]=phamau;
			else if((k-3)%4==0)
				color[k]=1.0f;
			else
				if(k>10)
					color[k]=phatrang;
			phamau-=0.001f;
			phatrang-=0.01;
		}
		}
		if (idcolor==2)
		{
		for(int k=0;k<numradius*4;k++){
			if((k-1)%4==0)
				color[k]=phamau;
			else if((k-3)%4==0)
				color[k]=1.0f;
			else
				if(k>10)
					color[k]=phatrang;
			phamau-=0.001f;
			phatrang-=0.01;
		}
		}
		
		if (idcolor==3)
		{
		for(int k=0;k<numradius*4;k++){
			if((k-2)%4==0)
				color[k]=phamau;
			else if((k-3)%4==0)
				color[k]=1.0f;
			else
				if(k>10)
					color[k]=phatrang;
			phamau-=0.001f;
			phatrang-=0.01;
		}
		}
		
		if (idcolor==4)
		{
		for(int k=0;k<numradius*4;k++){
			if((k-3)%4==0)
				color[k]=phamau;
			else if((k-3)%4==0)
				color[k]=1.0f;
			else
				if(k>10)
					color[k]=phatrang;
			phamau-=0.001f;
			phatrang-=0.01;
		}
		}
		
		if (idcolor==5)
		{
		for(int k=0;k<numradius*4;k++){
			if((k-4)%5==0)
				color[k]=phamau;
			else if((k-3)%4==0)
				color[k]=1.0f;
			else
				if(k>10)
					color[k]=phatrang;
			phamau-=0.001f;
			phatrang-=0.01;
		}
		}
		ByteBuffer vbb5=ByteBuffer.allocateDirect(color.length*4);
		vbb5.order(ByteOrder.nativeOrder());
		cb=vbb5.asFloatBuffer();
		cb.put(color);
		cb.position(0);
	}
	public void draw(GL10 gl)
	{
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, fb);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0,cb );
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		
	
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, numradius);
	}
	
}
